package frame;

import tree.*;
import sqlmanager.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import dialogs.*;
import panels.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JMenuItem;
import java.awt.event.KeyEvent;

public class MainFrame extends JFrame {
	// main toolbar 's buttons actionCommand
	private final String OpenAC = "open";
	private final String CloseAC = "close";
	// main menu 's menu items actionCommand
	private final String ExitAC = "exit";
	private final String AboutAC = "about";
	private final String ObjectBrowserAC = "object browser";
	private final String DataBrowserAC = "fieldInfos browser";
	private final String SQLEditorAC = "sql editor";
	private final String DBMSInfoAC = "dbms info";
	private final String DBMSFunctionsAC = "dbms func";
	private final String DBMSTypesAC = "dbms types";
	private final String SysInfoAC = "system info";
	// sql editor's toolbar's buttons' actionCommand
	private final String RunAC = "run";
	private JPanel pane = null;
	private JPanel treeViewPanel = null;
	private JScrollPane treeViewScrollPane = null;
	private JSplitPane centerSplitPane = null;
	private JTabbedPane contentTabbedPane = null;
	private JMenuBar mainMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenu viewMenu = null;
	private JMenuItem objectBrowserMenuItem = null;
	private JMenuItem dataBrowserMenuItem = null;
	private JMenuItem sqlEditorMenuItem = null;
	private JMenuItem dbmsInfoMenuItem = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem openMenuItem = null;
	private JMenuItem systemInfoMenuItem = null;
	private JMenuItem closeMenuItem = null;
	private JMenuItem dbmsFunctionsMenuItem = null;
	private JToolBar mainToolBar = null;
	private JToolBar pathToolBar = null;
	private JButton openButton = null;
	private JButton closeButton = null;
	private JTree tree = null;
	private RootNode root = null;
	private DefaultTreeModel treeModel = null;
	private JTextField pathTextField = null;
	private JLabel treeLabel = null;
	private JLabel pathLabel = null;
	private ObjectBrowserPanel objectBrowserPanel = null;
	private DataBrowserPanel dataBrowserPanel = null;
	private SQLEditorPanel sqlEditorPanel = null;

	private ActionHandler actionHandler = null;
	private DatabaseSelectListener treeSelectionListener = null;
	private SQLManager sqlManager = null;
	private OpenDialog openDialog = null;
	private InfoDialog infoDialog = null;
	private DetailDialog detailDialog = null;
	private String catalog = "";
	private String tablename = "";
	private boolean isConnected = false;
	private JMenuItem dbmsTypesMenuItem = null;

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		initialize();
		this.setConnected(false);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar(getMainMenuBar());
		this.setSize(650, 520);
		this.setContentPane(getPane());
		this.setTitle("SQL Front");
	}

	private SQLManager getSqlManager() {
		if (null == sqlManager)
			sqlManager = new SQLManager();
		return sqlManager;
	}

	public void connectTo(Authentification auth) {
		try {
			this.getSqlManager().connectTo(auth);
			List catalogs = this.sqlManager.getCatalogs();
			// update tree view
			root.setUserObject(auth.source);
			TreeFactory.addNodes(this.treeModel, this.root, catalogs);
			this.tree.expandPath(new TreePath(root));
			this.setConnected(true);
		} catch (SQLException e) {
			System.out.println("���ӵ����Դ��ȡ�ñ���ʧ��.");
			e.printStackTrace();
		}
	}

	public void close() {
		this.getSqlManager().close();
		this.pathTextField.setText("");
		this.root.clear();
		this.treeModel.reload();
		this.objectBrowserPanel.clear();
		this.dataBrowserPanel.clear();
		this.sqlEditorPanel.clear();
		this.setConnected(false);
	}

	private void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
		if (false == this.isConnected) {
			this.openMenuItem.setEnabled(true);
			this.closeMenuItem.setEnabled(false);
			this.dbmsInfoMenuItem.setEnabled(false);
			this.dbmsFunctionsMenuItem.setEnabled(false);
			this.dbmsTypesMenuItem.setEnabled(false);
			this.openButton.setEnabled(true);
			this.closeButton.setEnabled(false);
			this.pathLabel.setEnabled(false);
			this.pathTextField.setEnabled(false);
			this.objectBrowserPanel.disableAll();
			this.dataBrowserPanel.disableAll();
			this.sqlEditorPanel.disableAll();
		} else {
			this.openMenuItem.setEnabled(false);
			this.closeMenuItem.setEnabled(true);
			this.dbmsInfoMenuItem.setEnabled(true);
			this.dbmsFunctionsMenuItem.setEnabled(true);
			this.dbmsTypesMenuItem.setEnabled(true);
			this.openButton.setEnabled(false);
			this.closeButton.setEnabled(true);
			this.pathLabel.setEnabled(true);
			this.pathTextField.setEnabled(true);
			this.objectBrowserPanel.enableAll();
			this.objectBrowserPanel.setSupportedTypes(this.getSqlManager().getDBMSTypeNames());
			this.dataBrowserPanel.enableAll();
			this.sqlEditorPanel.enableAll();
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getMainToolBar(), java.awt.BorderLayout.NORTH); // Generated
			pane.add(getCenterSplitPane(), java.awt.BorderLayout.CENTER); // Generated
		}
		return pane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new JMenuBar();
			mainMenuBar.add(getFileMenu());
			mainMenuBar.add(getEditMenu());
			mainMenuBar.add(getViewMenu()); // Generated
			mainMenuBar.add(getHelpMenu());
		}
		return mainMenuBar;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.setMnemonic(java.awt.event.KeyEvent.VK_F); // Generated
			fileMenu.add(getOpenMenuItem());
			fileMenu.add(getCloseMenuItem()); // Generated
			fileMenu.addSeparator();
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.setMnemonic(java.awt.event.KeyEvent.VK_E); // Generated
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.setMnemonic(java.awt.event.KeyEvent.VK_H); // Generated
			helpMenu.add(getSystemInfoMenuItem()); // Generated
			helpMenu.addSeparator();
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_X); // Generated
			exitMenuItem.setActionCommand(this.ExitAC);
			exitMenuItem.addActionListener(this.getActionHandler());
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_A); // Generated
			aboutMenuItem.setActionCommand(this.AboutAC);
			aboutMenuItem.addActionListener(this.getActionHandler());
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getCloseMenuItem() {
		if (closeMenuItem == null) {
			closeMenuItem = new JMenuItem();
			closeMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_C); // Generated
			closeMenuItem.setText("Close"); // Generated
			closeMenuItem.setActionCommand(this.CloseAC); // Generated
			closeMenuItem.addActionListener(this.getActionHandler());
		}
		return closeMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getOpenMenuItem() {
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem();
			openMenuItem.setText("Open");
			openMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_O); // Generated
			openMenuItem.setActionCommand(this.OpenAC);
			openMenuItem.addActionListener(this.getActionHandler());
		}
		return openMenuItem;
	}

	/**
	 * This method initializes viewMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getViewMenu() {
		if (viewMenu == null) {
			viewMenu = new JMenu();
			viewMenu.setMnemonic(java.awt.event.KeyEvent.VK_V); // Generated
			viewMenu.setText("View"); // Generated
			viewMenu.setPreferredSize(new java.awt.Dimension(35, 23)); // Generated
			viewMenu.add(getShowObjectBrowserMenuItem()); // Generated
			viewMenu.add(getDataBrowserMenuItem()); // Generated
			viewMenu.add(getSqlEditorMenuItem()); // Generated
			viewMenu.addSeparator();
			viewMenu.add(getDbmsInfoMenuItem()); // Generated
			viewMenu.add(getDbmsFunctionsMenuItem()); // Generated
			viewMenu.add(getJMenuItem()); // Generated
		}
		return viewMenu;
	}

	/**
	 * This method initializes showObjectBrowserMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getShowObjectBrowserMenuItem() {
		if (objectBrowserMenuItem == null) {
			objectBrowserMenuItem = new JMenuItem();
			objectBrowserMenuItem.setText("Object Browser"); // Generated
			objectBrowserMenuItem.setActionCommand(this.ObjectBrowserAC);
			objectBrowserMenuItem.addActionListener(this.getActionHandler());
			objectBrowserMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_O); // Generated
		}
		return objectBrowserMenuItem;
	}

	/**
	 * This method initializes systemInfoMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getSystemInfoMenuItem() {
		if (systemInfoMenuItem == null) {
			systemInfoMenuItem = new JMenuItem();
			systemInfoMenuItem.setText("System Information"); // Generated
			systemInfoMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_S); // Generated
			systemInfoMenuItem.setActionCommand(this.SysInfoAC);
			systemInfoMenuItem.addActionListener(this.getActionHandler());
		}
		return systemInfoMenuItem;
	}

	/**
	 * This method initializes dataBrowserMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getDataBrowserMenuItem() {
		if (dataBrowserMenuItem == null) {
			dataBrowserMenuItem = new JMenuItem();
			dataBrowserMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_D); // Generated
			dataBrowserMenuItem.setText("Data Browser"); // Generated
			dataBrowserMenuItem.setActionCommand(this.DataBrowserAC);
			dataBrowserMenuItem.addActionListener(this.getActionHandler());
		}
		return dataBrowserMenuItem;
	}

	/**
	 * This method initializes sqlEditorMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getSqlEditorMenuItem() {
		if (sqlEditorMenuItem == null) {
			sqlEditorMenuItem = new JMenuItem();
			sqlEditorMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_S); // Generated
			sqlEditorMenuItem.setText("SQL Editor"); // Generated
			sqlEditorMenuItem.setActionCommand(this.SQLEditorAC);
			sqlEditorMenuItem.addActionListener(this.getActionHandler());
		}
		return sqlEditorMenuItem;
	}

	/**
	 * This method initializes dbmsInfoMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getDbmsInfoMenuItem() {
		if (dbmsInfoMenuItem == null) {
			dbmsInfoMenuItem = new JMenuItem();
			dbmsInfoMenuItem.setText("DBMS Information"); // Generated
			dbmsInfoMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_I); // Generated
			dbmsInfoMenuItem.setActionCommand(this.DBMSInfoAC);
			dbmsInfoMenuItem.addActionListener(this.getActionHandler());
		}
		return dbmsInfoMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getDbmsFunctionsMenuItem() {
		if (dbmsFunctionsMenuItem == null) {
			dbmsFunctionsMenuItem = new JMenuItem();
			dbmsFunctionsMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_F); // Generated
			dbmsFunctionsMenuItem.setText("DBMS Functions"); // Generated
			dbmsFunctionsMenuItem.setActionCommand(this.DBMSFunctionsAC); // Generated
			dbmsFunctionsMenuItem.addActionListener(this.getActionHandler());
		}
		return dbmsFunctionsMenuItem;
	}

	/**
	 * This method initializes jMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItem() {
		if (dbmsTypesMenuItem == null) {
			dbmsTypesMenuItem = new JMenuItem();
			dbmsTypesMenuItem.setMnemonic(java.awt.event.KeyEvent.VK_Y); // Generated
			dbmsTypesMenuItem.setText("DBMS Types"); // Generated
			dbmsTypesMenuItem.setActionCommand(this.DBMSTypesAC); // Generated
			dbmsTypesMenuItem.addActionListener(this.getActionHandler());
		}
		return dbmsTypesMenuItem;
	}

	/**
	 * This method initializes mainToolBarBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getMainToolBar() {
		if (mainToolBar == null) {
			pathLabel = new JLabel();
			pathLabel.setText("·�� "); // Generated
			pathLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 14)); // Generated
			mainToolBar = new JToolBar();
			mainToolBar.add(getOpenButton()); // Generated
			mainToolBar.add(getCloseButton()); // Generated
			mainToolBar.add(new JToolBar.Separator(new Dimension(16, 16)));
			mainToolBar.add(getPathToolBar()); // Generated
		}
		return mainToolBar;
	}

	/**
	 * This method initializes connectButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOpenButton() {
		if (openButton == null) {
			openButton = new JButton();
			openButton.setText("Open"); // Generated
			openButton.setMnemonic(java.awt.event.KeyEvent.VK_O); // Generated
			openButton.setActionCommand(this.OpenAC);
			openButton.addActionListener(this.getActionHandler());
		}
		return openButton;
	}

	/**
	 * This method initializes pathToolBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getPathToolBar() {
		if (pathToolBar == null) {
			pathLabel = new JLabel();
			pathLabel.setText("·�� "); // Generated
			pathLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14)); // Generated
			pathToolBar = new JToolBar();
			pathToolBar.add(pathLabel); // Generated
			pathToolBar.add(getPathTextField()); // Generated
		}
		return pathToolBar;
	}

	/**
	 * This method initializes goButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close"); // Generated
			closeButton.setMnemonic(java.awt.event.KeyEvent.VK_C); // Generated
			closeButton.setActionCommand(this.CloseAC);
			closeButton.addActionListener(this.getActionHandler());
		}
		return closeButton;
	}

	/**
	 * This method initializes centerSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getCenterSplitPane() {
		if (centerSplitPane == null) {
			centerSplitPane = new JSplitPane();
			centerSplitPane.setDividerSize(5); // Generated
			centerSplitPane.setRightComponent(getContentTabbedPane()); // Generated
			centerSplitPane.setLeftComponent(getTreeViewPanel()); // Generated
			centerSplitPane.setResizeWeight(0.35D); // Generated
		}
		return centerSplitPane;
	}

	/**
	 * This method initializes jTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getContentTabbedPane() {
		if (contentTabbedPane == null) {
			contentTabbedPane = new JTabbedPane();
			contentTabbedPane.addTab("Object Browser", null, getObjectBrowserPanel(), null); // Generated
			contentTabbedPane.addTab("Data Browser", null, getDataBrowserPanel(), null); // Generated
			contentTabbedPane.addTab("SQL Editor", null, getSqlEditorPanel(), null); // Generated
		}
		return contentTabbedPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private ObjectBrowserPanel getObjectBrowserPanel() {
		if (objectBrowserPanel == null) {
			objectBrowserPanel = new ObjectBrowserPanel();
		}
		return objectBrowserPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private DataBrowserPanel getDataBrowserPanel() {
		if (dataBrowserPanel == null) {
			dataBrowserPanel = new DataBrowserPanel();
		}
		return dataBrowserPanel;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private SQLEditorPanel getSqlEditorPanel() {
		if (sqlEditorPanel == null) {
			sqlEditorPanel = new SQLEditorPanel(this.getSqlManager());
		}
		return sqlEditorPanel;
	}

	/**
	 * This method initializes treeViewPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTreeViewPanel() {
		if (treeViewPanel == null) {
			treeLabel = new JLabel();
			treeLabel.setText("��Դ"); // Generated
			treeViewPanel = new JPanel();
			treeViewPanel.setLayout(new BorderLayout()); // Generated
			treeViewPanel.add(treeLabel, java.awt.BorderLayout.NORTH); // Generated
			treeViewPanel.add(getTreeViewScrollPane(), java.awt.BorderLayout.CENTER); // Generated
		}
		return treeViewPanel;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTreeViewScrollPane() {
		if (treeViewScrollPane == null) {
			treeViewScrollPane = new JScrollPane();
			treeViewScrollPane.setMinimumSize(new java.awt.Dimension(125, 50)); // Generated
			treeViewScrollPane.setViewportView(getTree()); // Generated
		}
		return treeViewScrollPane;
	}

	/**
	 * This method initializes jTree
	 * 
	 * @return javax.swing.JTree
	 */
	private JTree getTree() {
		if (tree == null) {

			tree = new JTree(this.getTreeModel());
			tree.setRootVisible(true);
			tree.setToggleClickCount(2); // Generated
			tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
			tree.addTreeSelectionListener(getTreeSelectionListener());
			tree.setCellRenderer(new DatabaseTreeCellRenderer());
			tree.addMouseListener(new DatabaseTreeMouseListener());
		}
		return tree;
	}

	private DefaultTreeModel getTreeModel() {
		if (null == treeModel)
			treeModel = new DefaultTreeModel(this.getTreeRoot());
		return treeModel;
	}

	private RootNode getTreeRoot() {
		if (null == root)
			root = new RootNode("");
		return root;
	}

	/**
	 * This method initializes pathTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPathTextField() {
		if (pathTextField == null) {
			pathTextField = new JTextField();
		}
		return pathTextField;
	}

	private OpenDialog getOpenDialog() {
		if (null == openDialog) {
			openDialog = new OpenDialog(this);
			Point floc = this.getLocation();
			openDialog.setLocation(floc.x + (this.getWidth() - openDialog.getWidth()) / 2, floc.y + (this.getHeight() - openDialog.getHeight()) / 2);
		}
		return openDialog;
	}

	private InfoDialog getInfoDialog() {
		if (null == infoDialog)
			infoDialog = new InfoDialog();
		return infoDialog;
	}

	private DetailDialog getDetailDialog() {
		if (null == detailDialog)
			detailDialog = new DetailDialog();
		return detailDialog;
	}

	private ActionHandler getActionHandler() {
		if (null == actionHandler)
			actionHandler = new ActionHandler();
		return actionHandler;
	}

	private DatabaseSelectListener getTreeSelectionListener() {
		if (null == treeSelectionListener)
			treeSelectionListener = new DatabaseSelectListener();
		return treeSelectionListener;
	}

	private void updatePath() {
		if (0 != this.tablename.length())
			this.pathTextField.setText(this.catalog + "." + this.tablename);
		else
			this.pathTextField.setText(this.catalog);
	}

	private void setSelectedCatalog(CatalogNode node) {
		this.catalog = (String) node.getUserObject();
		this.tablename = "";
		try {
			this.dataBrowserPanel.clear();
			List tableNames = sqlManager.getTableNames(catalog);
			TreeFactory.addNodes(this.treeModel, node, tableNames);
			updatePath();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void setSelectedTable(TableNode node) {
		try {
			this.catalog = ((AbstractTreeNode) node.getParent()).getUserObject().toString();
			this.tablename = node.getUserObject().toString();
			List fieldNames = sqlManager.getTableContents(catalog, tablename, this.dataBrowserPanel.tableModel.contents,
					this.objectBrowserPanel.tableModel.fieldInfos, this.dataBrowserPanel.tableModel.fieldNames,
					this.objectBrowserPanel.tableModel.primaryKeys);
			this.dataBrowserPanel.tableModel.fireTableStructureChanged();
			this.objectBrowserPanel.tableModel.updatePrimaryKeyInfo();
			this.objectBrowserPanel.tableModel.setupColumnRenderer(objectBrowserPanel.table);
			this.objectBrowserPanel.tableModel.fireTableDataChanged();
			TreeFactory.addNodes(this.treeModel, node, fieldNames);
			updatePath();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("ִ��ѡ�� " + catalog + "." + tablename + " ����ʱ�����쳣");
		}
	}

	private void setSelectedField(FieldNode node) {
		TableNode tableNode = (TableNode) node.getParent();
		CatalogNode catalogNode = (CatalogNode) tableNode.getParent();
		if (false == this.catalog.equals(catalogNode.getUserObject()) || false == this.tablename.equals(tableNode.getUserObject()))
			this.setSelectedTable(tableNode);
		objectBrowserPanel.setSelectField((String) node.getUserObject());
	}

	class DatabaseSelectListener implements TreeSelectionListener {
		public void valueChanged(TreeSelectionEvent e) {
			TreePath path = e.getNewLeadSelectionPath();
			if (null == path)
				return;
			AbstractTreeNode node = (AbstractTreeNode) path.getLastPathComponent();
			if (null == node)
				return;

			switch (node.getNodeType()) {
			case AbstractTreeNode.RootNode:
				break;
			case AbstractTreeNode.CatalogNode:
				setSelectedCatalog((CatalogNode) node);
				break;
			case AbstractTreeNode.TableNode:
				setSelectedTable((TableNode) node);
				break;
			case AbstractTreeNode.FieldNode:
				setSelectedField((FieldNode) node);
				break;
			default:
				System.out.println("valueChanged����: ����Ľڵ�����");
			}
			System.out.println(node.toString());
		}
	}

	class DatabaseTreeMouseListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			if (-1 == tree.getRowForLocation(e.getX(), e.getY()))
				return;
			// �����˫��,��ʾdataBrowserPanel;���ǵ���,��ʾobjectBrowserPanel
			if (1 == e.getClickCount()) {
				contentTabbedPane.setSelectedComponent(objectBrowserPanel);
			} else {
				// AbstractTreeNode node = (AbstractTreeNode)
				// tree.getPathForLocation(
				// e.getX(), e.getY() ).getLastPathComponent();
				// if( AbstractTreeNode.TableNode == node.getNodeType() ||
				// AbstractTreeNode.FieldNode == node.getNodeType() )
				contentTabbedPane.setSelectedComponent(dataBrowserPanel);
			}
		}
	}

	class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ac = e.getActionCommand();
			if (ac.equals(OpenAC)) {
				getOpenDialog().setVisible(true);
			} else if (ac.equals(CloseAC)) {
				close();
			} else if (ac.equals(RunAC)) {
				System.out.println("Run Button Pressed.");
			} else if (ac.equals(ObjectBrowserAC)) {
				getContentTabbedPane().setSelectedComponent(objectBrowserPanel);
			} else if (ac.equals(DataBrowserAC)) {
				getContentTabbedPane().setSelectedComponent(dataBrowserPanel);
			} else if (ac.equals(SQLEditorAC)) {
				getContentTabbedPane().setSelectedComponent(sqlEditorPanel);
			} else if (ac.equals(DBMSFunctionsAC)) {
				sqlManager.getDBMSDetail(getDetailDialog().topicTableModel.data);
				detailDialog.showDBMSDetail(getLocation(), getSize());
			} else if (ac.equals(DBMSTypesAC)) {
				sqlManager.getDBMSTypes(getDetailDialog().topicTableModel.data);
				detailDialog.showDBMSTypes(getLocation(), getSize());
			} else if (ac.equals(DBMSInfoAC)) {
				// show dbms information
				sqlManager.getDBMSInfo(getInfoDialog().tableModel.data);
				infoDialog.showDBMSInformation(getLocation(), getSize());
			} else if (ac.equals(SysInfoAC)) {
				// show system information
				getInfoDialog().showSystemInformation(getLocation(), getSize());
			} else if (ac.equals(AboutAC)) {
				JOptionPane.showMessageDialog(MainFrame.this, "<html><body>" + "<h3>This is a simple SQL Front demo<h4 color = "
						+ "blue align = right>code by svchost</body></html>", "About", JOptionPane.INFORMATION_MESSAGE);
			} else if (ac.equals(ExitAC)) {
				// �ͷ�������Դ
				close();
				System.exit(0);
			}
		}
	}
}
